define(function(require, exports, module) {
    require('basic');
    require('jquerytmpl');
    var config = require('config');
    var model = function(){
        this.init();
    };
    model.prototype ={
        info: {
            token: null,
            leadPost: null
        },
        setInfo: function(_str){
            var sf = this;
            this.info.token = _str;
            this.info.leadPost = '?token='+_str;
            this.info.checkInfoBL = true;
            $.cookie('_token',_str,{path:'/'});
            if($.cookie('_username')){
                $('.username_view').text($.cookie('_username'));
            };
        },
        checkInfo: function(){
            var sf = this;
            var token = $.cookie('_token') || this.info.token;
            if(token){
                var data_str = '?token='+token;
                sf.checkAjax = $.ajax({
                    type: "GET",
                    url: config.login_check+data_str,
                    dataType: 'json',
                    success: function(data){
                        if(data){
                            window.location.href = 'index.php';
                        }else{
                            sf.getDcList();
                        }
                    } 
                });
            }else{
                sf.getDcList();
            };
        },
        /**
         * 设置选择的数据中心，选择后应该要刷新页面，选择的数据中心应该记录在cookie里
         * @param {[string]} str 数据中心的name字段
         */
        setDc: function(_str){
            var sf = this;
            var data_str = this.info.leadPost+'&name='+_str;
            $.ajax({
                type: "POST",
                url: config.dc_select+data_str,
                dataType: 'json',
                success: function(data){
                    $.cookie('_dc',_str,{path:'/'});
                    window.location.href = 'index.php';
                },
                error: function(e){
                    $('#error_body').html('').html(e.responseText);
                    $('#error').modal({keyboard: false, show: true});
                }
            });
        },
        /**
         * 获取数据中心信息,并设置一个默认的数据中心到cookie里
         * @return {[type]} [description]
         */
        getDcList: function(){
            var sf = this;
            $.ajax({
                type: "GET",
                url: config.dc_list,
                dataType: 'json',
                success: function(data){
                    $("#dcListTemplateForLogin").tmpl(data).appendTo("#dcListLogin");
                    $('#loginForm .btn_submit').off('click').on('click',function(e){
                        var loginForm = $('#loginForm'),
                            obj = {};
                        loginForm.find('.form-control').each(function(index,el){
                            if(index!=2){
                                obj[$(el).attr('name')] = $(el).val();
                            }
                            
                        });
                        var postData = JSON.stringify(obj);
                        $.ajax({
                            type: "POST",
                            url: config.login,
                            data: postData,
                            dataType: 'json',
                            success: function(data){
                                var now = new Date();
                                $.cookie('_login_date',now,{path:'/'});
                                $.cookie('_username',obj['username'],{path:'/'}); //设置username的cookie
                                sf.setInfo(data);
                                var dc_str = $('#dcListLogin').val();
                                sf.setDc(dc_str);
                            },
                            error: function(e){
                                $('#error_body').html('').html(e.responseText);
                                $('#error').modal({keyboard: false, show: true});
                            }
                        });
                    });
                }
            });
        },
        init: function(){
            var sf = this;
            this.checkInfo();
        }
    };
    module.exports = new model();
});

